  
package com.demo.demos.demo1.controller;

import com.demo.demos.demo1.model.po.User;
import com.demo.demos.demo1.model.query.UserPQuery;
import com.demo.demos.demo1.service.UserService;
import com.demo.result.PageDTO;
import com.demo.result.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;



@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class UserController {


    private final UserService userService;

    @PostMapping("/save")
    Result<User> save(@RequestBody User user) {
        User res = userService.saveUser(user);
        return Result.success(res);
    }

    @DeleteMapping("/{id}")
    Result<String> deleteApply(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    User get(@PathVariable Long id) {
        return userService.getById(id);
    }

    @PutMapping("/put")
    Result<User> update(@RequestBody User user) {
        User res =  userService.updateUserById(user);
        return Result.success(res);
    }

    @GetMapping("/page")
    Result<PageDTO<User>> page(UserPQuery userQuery) {
        PageDTO<User> pageResult = userService.pageQuery(userQuery);
        return Result.success(pageResult);
    }
}